//对axios进行二次封装
import axios from "axios";
import { Toast } from "vant";

//实例化axios
var service = axios.create({
    baseURL:'/api',//基准路径
    timeout:10000
})

//设置请求拦截器
service.interceptors.request.use((config)=>{
    //统一设置请求头
    config.headers['Authorization'] = 'Bearer' + '' + localStorage.getItem('token');
    //显示loading
    Toast.loading({message:'loading...',duration:0});

    return config;
},(error)=>{
    return Promise.reject(error);
});

//设置响应拦截器
service.interceptors.response.use((res)=>{
    Toast.clear();//隐藏loading
    return res;
},(error)=>{
    Toast.clear();
    if(error.response.status == 401){
        Toast.fail({message: "身份认证失败,登陆过期,请重新登录!"})
    }
    else if(error.response.status == 404){
        Toast.fail({ message: "访问路径有误!" })
    }
    else if(error.response.status == 500){
        Toast.fail({ message: "服务器内部错误!" })
    }
    return Promise.reject(error);
});

export default service;